package controller;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;

public class JSONSender 
{
	private final static String LIGHT_KEY = "light";
	private final static String STATE_KEY = "state";
	
	private static JSONArray _jsonArray = new JSONArray();
	
	/**
	 * When a trafficlight state changes, use this methode to add it to the jsonarray.
	 * @param light The light that changed state, eg: "N1"
	 * @param state The new state of the light, eg: "red"
	 */
	public static void addToSendList(String direction, int laneType, String state)
	{
		JSONObject jo = new JSONObject();
		String light = direction + laneType;
		jo.put(LIGHT_KEY, light);
		jo.put(STATE_KEY, state);	
		_jsonArray.add(jo);
	}
	
	/**
	 * Sends all the jsonbojects which are added to the array, if the array is empty, nothing is send.
	 */
	public static void sendArray()
	{
		if(_jsonArray.size() > 0)
		{
			Controller.send(_jsonArray.toJSONString());
			_jsonArray.clear();
		}
	}
}
